﻿#coding=utf-8
#Boa:Frame:FrameTableSO

import wx
import wx.grid
import  wx.lib.printout as  printout

def create(parent):
    return FrameTableSO(parent)

[wxID_FRAMETABLESO, wxID_FRAMETABLESOBUTTON1, wxID_FRAMETABLESOBUTTON2, 
 wxID_FRAMETABLESOGRID1, wxID_FRAMETABLESOSTATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(5)]

class FrameTableSO(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMETABLESO, name=u'FrameTableSO',
              parent=prnt, pos=wx.Point(69, 296), size=wx.Size(1083, 490),
              style=wx.DEFAULT_FRAME_STYLE, title=u'FrameTableSO')
        self.SetClientSize(wx.Size(1075, 456))

        self.button1 = wx.Button(id=wxID_FRAMETABLESOBUTTON1, label='button1',
              name='button1', parent=self, pos=wx.Point(88, 40),
              size=wx.Size(56, 64), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAMETABLESOBUTTON1)

        self.staticText1 = wx.StaticText(id=wxID_FRAMETABLESOSTATICTEXT1,
              label=u'\u6253\u5370\u6d4b\u8bd5', name='staticText1',
              parent=self, pos=wx.Point(200, 88), size=wx.Size(48, 14),
              style=0)

        self.grid1 = wx.grid.Grid(id=wxID_FRAMETABLESOGRID1, name='grid1',
              parent=self, pos=wx.Point(40, 144), size=wx.Size(992, 232),
              style=0)

        self.button2 = wx.Button(id=wxID_FRAMETABLESOBUTTON2, label='button2',
              name='button2', parent=self, pos=wx.Point(352, 40),
              size=wx.Size(75, 24), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
              id=wxID_FRAMETABLESOBUTTON2)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        titles = [u'编号', u'产品', u'单位', u'数量', u'单价', u'小计',u'']
        self.grid1.CreateGrid(10, len(titles))
        for i in range(len(titles)):
            self.grid1.SetColLabelValue(i, titles[i])
        
##        for i in range(10):
##            for j in range(7):
##               self.grid1.SetCellValue(i,j,str(i*j)) 
        self.grid1.SetCellSize(1,1,2,3)
        self.grid1.SetRowSize(1, 50)
        
    def OnButton1Button(self, event):
        data = wx.PrintDialogData()
        printout = MyPrintout(self)
        printout2 = MyPrintout(self)
        self.preview = wx.PrintPreview(printout, printout2, data)

        if not self.preview.Ok():
            self.log.WriteText("Houston, we have a problem...\n")
            return

        pfrm = wx.PreviewFrame(self.preview, self, "This is a print preview")

        pfrm.Initialize()
        pfrm.SetPosition(self.GetPosition())
        pfrm.SetSize(self.GetSize())
        pfrm.Show(True)
        event.Skip()
    
    def getWidth(self):
        return 400

    def getHeight(self):
        return 400

    def OnButton2Button(self, event):
        ptbl = printout.PrintGrid(self,self.grid1)
        ptbl.Preview()
    
class MyPrintout(wx.Printout):
    def __init__(self, canvas):
        wx.Printout.__init__(self)
        self.canvas = canvas


    def OnBeginDocument(self, start, end):

        return super(MyPrintout, self).OnBeginDocument(start, end)

    def OnEndDocument(self):

        super(MyPrintout, self).OnEndDocument()

    def OnBeginPrinting(self):

        super(MyPrintout, self).OnBeginPrinting()

    def OnEndPrinting(self):

        super(MyPrintout, self).OnEndPrinting()

    def OnPreparePrinting(self):

        super(MyPrintout, self).OnPreparePrinting()

    def HasPage(self, page):

        if page <= 2:
            return True
        else:
            return False

    def GetPageInfo(self):

        return (1, 2, 1, 2)

    def OnPrintPage(self, page):

        dc = self.GetDC()

        #-------------------------------------------
        # One possible method of setting scaling factors...

        maxX = self.canvas.getWidth()
        maxY = self.canvas.getHeight()

        # Let's have at least 50 device units margin
        marginX = 50
        marginY = 50

        # Add the margin to the graphic size
        maxX = maxX + (2 * marginX)
        maxY = maxY + (2 * marginY)

        # Get the size of the DC in pixels
        (w, h) = dc.GetSizeTuple()

        # Calculate a suitable scaling factor
        scaleX = float(w) / maxX
        scaleY = float(h) / maxY

        # Use x or y scaling factor, whichever fits on the DC
        actualScale = min(scaleX, scaleY)

        # Calculate the position on the DC for centering the graphic
        posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
        posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0

        # Set the scale and origin
        dc.SetUserScale(actualScale, actualScale)
        dc.SetDeviceOrigin(int(posX), int(posY))

        #-------------------------------------------

        #self.canvas.DoDrawing(dc, True)
        dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)

        return True
